//
//  LandObject.cpp
//  StarBugs
//
//  Created by jinkwon jeong on 12/13/12.
//  Copyright (c) 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/LandObject.h"
#include "Util/ImageManager.h"
#include "Util/UserData.h"

#pragma mark archObstacle

ArcObstacle::~ArcObstacle()
{
    
}
ArcObstacle::ArcObstacle()
{
    
    
    char _name[32];
    //    if(UserData::getInstencePtr()->chapter < 5)
    sprintf(_name, "ch%d_rock_circle", UserData::getInstencePtr()->chapter + 1);
    //    else
    //        sprintf(_name, "ch%d_rock_circle", 5);
    mainSprite = ImageManager::getSpriteWithXml2(kImageSheet_rockImage, _name);
    
    
    addChild(mainSprite);
    objectType = objectType_Arc;
}

void ArcObstacle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.4f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-36/PTM_RATIO, -27/PTM_RATIO), b2Vec2(-29/PTM_RATIO, -38/PTM_RATIO), b2Vec2(2/PTM_RATIO, -38/PTM_RATIO), b2Vec2(33/PTM_RATIO, -15/PTM_RATIO),b2Vec2(38/PTM_RATIO, 13/PTM_RATIO),
        b2Vec2(33/PTM_RATIO, 29/PTM_RATIO),b2Vec2(19/PTM_RATIO,  37/PTM_RATIO), b2Vec2(21/PTM_RATIO, 18/PTM_RATIO),b2Vec2(20/PTM_RATIO, 10/PTM_RATIO),b2Vec2(17/PTM_RATIO, -2/PTM_RATIO),
        b2Vec2(10/PTM_RATIO, -10/PTM_RATIO),b2Vec2(2/PTM_RATIO, -18/PTM_RATIO),b2Vec2(-9/PTM_RATIO, -23/PTM_RATIO),b2Vec2(-21/PTM_RATIO, -27/PTM_RATIO),b2Vec2(-36/PTM_RATIO, -27/PTM_RATIO),
        
        //        b2Vec2(294, 151),b2Vec2(278, 185), b2Vec2(250, 194), b2Vec2(212, 97), b2Vec2(168, 63),
        //        b2Vec2(115, 33), b2Vec2(60, 16),
        
    };
    poly.CreateChain(vertices, 15);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void ArcObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void ArcObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 5;
    else
        bodyAngle -= 5;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark shortRock

ShortRockObstacle::~ShortRockObstacle()
{
    
}
ShortRockObstacle::ShortRockObstacle()
{
    
    char _name[32];
    //    if(UserData::getInstencePtr()->chapter < 5)
    sprintf(_name, "ch%d_rock_smallFlat", UserData::getInstencePtr()->chapter + 1);
    //    else
    //        sprintf(_name, "ch%d_rock_smallFlat", 5);
    
    mainSprite = ImageManager::getSpriteWithXml2(kImageSheet_rockImage, _name);
    //    mainSprite->setScale(0.8);
    addChild(mainSprite);
    objectType = objectType_shortFlat;
}

void ShortRockObstacle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-25/PTM_RATIO, 17/PTM_RATIO), b2Vec2(-24/PTM_RATIO, -17/PTM_RATIO), b2Vec2(1/PTM_RATIO, -39/PTM_RATIO), b2Vec2(21/PTM_RATIO, -16/PTM_RATIO),b2Vec2(23/PTM_RATIO, 21/PTM_RATIO),
        b2Vec2(19/PTM_RATIO, 24/PTM_RATIO),b2Vec2(-25/PTM_RATIO, 17/PTM_RATIO)
        
    };
    poly.CreateChain(vertices, 7);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void ShortRockObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void ShortRockObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark longlock
LongRockObstacle::~LongRockObstacle()
{
    
}
LongRockObstacle::LongRockObstacle()
{
    char _name[32];
    sprintf(_name, "ch%d_rock_longFlat", UserData::getInstencePtr()->chapter + 1);
    mainSprite = ImageManager::getSpriteWithXml2(kImageSheet_rockImage, _name);
    addChild(mainSprite);
    objectType = objectType_longFlat;
}

void LongRockObstacle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-61/PTM_RATIO, 7/PTM_RATIO), b2Vec2(-38/PTM_RATIO, -10/PTM_RATIO), b2Vec2(-14/PTM_RATIO, -10/PTM_RATIO), b2Vec2(9/PTM_RATIO, -16/PTM_RATIO),b2Vec2(33/PTM_RATIO, -7/PTM_RATIO),
        b2Vec2(53/PTM_RATIO, -5/PTM_RATIO),b2Vec2(60/PTM_RATIO, 7/PTM_RATIO),b2Vec2(-61/PTM_RATIO, 7/PTM_RATIO)
        
    };
    poly.CreateChain(vertices, 8);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void LongRockObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void LongRockObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark tinyrock

TinyRockObstacle::~TinyRockObstacle()
{
    
}
TinyRockObstacle::TinyRockObstacle()
{
    frameArray = CCArray::create();
    frameArray->retain();
    frameMax = 0;
    num = 0;
    char _name[32];
    //    if(UserData::getInstencePtr()->chapter < 5)
    sprintf(_name, "ch%d_rock_tiny", UserData::getInstencePtr()->chapter + 1);
    //    else
    //        sprintf(_name, "ch%d_rock_tiny", 5);
    mainSprite = ImageManager::getSpriteWithXml2(kImageSheet_rockImage, _name);
    //    mainSprite->setScale(0.8);
    addChild(mainSprite);
    objectType = objectType_tinyRock;
}

void TinyRockObstacle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-10/PTM_RATIO, 11/PTM_RATIO), b2Vec2(-10/PTM_RATIO, -9/PTM_RATIO), b2Vec2(2/PTM_RATIO, -16/PTM_RATIO),
        b2Vec2(9/PTM_RATIO, -13/PTM_RATIO),b2Vec2(10/PTM_RATIO, 11/PTM_RATIO),b2Vec2(-10/PTM_RATIO, 11/PTM_RATIO),
    };
    poly.CreateChain(vertices, 6);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void TinyRockObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void TinyRockObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark wall
WallObstacle::~WallObstacle()
{
    
}
WallObstacle::WallObstacle()
{
    
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "wall");
    //    mainSprite->setScale(0.8);
    addChild(mainSprite);
    objectType = objectType_wall;
}

void WallObstacle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
	mainBody = world->CreateBody(&bodyDef);
	
    b2PolygonShape rec;
    rec.SetAsBox(10 / PTM_RATIO, 20 / PTM_RATIO);
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &rec;
    fixtureDef.restitution = 0.5f;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void WallObstacle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void WallObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark thin_long

Thin_long::~Thin_long()
{
    
}
Thin_long::Thin_long()
{
    
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_thin_long");
    
    addChild(mainSprite);
    objectType = objectType_thinlong;
}

void Thin_long::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-43/PTM_RATIO, 6/PTM_RATIO), b2Vec2(-43/PTM_RATIO, 0/PTM_RATIO), b2Vec2(-40/PTM_RATIO, -6/PTM_RATIO), b2Vec2(42/PTM_RATIO, -6/PTM_RATIO),b2Vec2(44/PTM_RATIO, 0/PTM_RATIO),
        b2Vec2(44/PTM_RATIO, 6/PTM_RATIO),b2Vec2(-43/PTM_RATIO, 6/PTM_RATIO)
        
    };
    poly.CreateChain(vertices, 7);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void Thin_long::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void Thin_long::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark thin_short

Thin_short::~Thin_short()
{
    
}
Thin_short::Thin_short()
{
    
    
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_thin_short");
    
    addChild(mainSprite);
    objectType = objectType_thinshort;
}

void Thin_short::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 1/000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    b2Vec2 vertices[] = {
        b2Vec2(-11/PTM_RATIO, 6/PTM_RATIO), b2Vec2(-9/PTM_RATIO, -6/PTM_RATIO), b2Vec2(10/PTM_RATIO, -6/PTM_RATIO), b2Vec2(12/PTM_RATIO, 6/PTM_RATIO),b2Vec2(-11/PTM_RATIO, 6/PTM_RATIO),
    };
    poly.CreateChain(vertices, 5);
    
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void Thin_short::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void Thin_short::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

#pragma mark thin_circle

Thin_circle::~Thin_circle()
{
    
}
Thin_circle::Thin_circle()
{
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_thin_circle");
    addChild(mainSprite);
    objectType = objectType_thinCircle;
}

void Thin_circle::setBody(b2World *world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
    
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
    
	mainBody = world->CreateBody(&bodyDef);
	
    b2ChainShape poly;
	b2FixtureDef fixtureDef;
    
    fixtureDef.density = 1.0f;
    fixtureDef.restitution = 0.4f;
    //    fixtureDef.friction = 1000.0f;
    //    fixtureDef.restitution = 0.5f;
    //    fixtureDef.friction = 0.4f;
    
    b2Vec2 vertices[] = {
        b2Vec2(-5/PTM_RATIO, -7/PTM_RATIO), b2Vec2(-21/PTM_RATIO, -13/PTM_RATIO), b2Vec2(-19/PTM_RATIO, -20/PTM_RATIO), b2Vec2(-2/PTM_RATIO, -17/PTM_RATIO), b2Vec2(16/PTM_RATIO, -2/PTM_RATIO),
        b2Vec2(20/PTM_RATIO, 18/PTM_RATIO),b2Vec2(12/PTM_RATIO, 21/PTM_RATIO), b2Vec2(8/PTM_RATIO, 6/PTM_RATIO),b2Vec2(-5/PTM_RATIO, -7/PTM_RATIO),
        
        //        b2Vec2(-20/PTM_RATIO, -12/PTM_RATIO), b2Vec2(-19/PTM_RATIO, -19/PTM_RATIO), b2Vec2(-10/PTM_RATIO, -20/PTM_RATIO), b2Vec2(-3/PTM_RATIO, -17/PTM_RATIO),b2Vec2(5/PTM_RATIO, -14/PTM_RATIO),
        //        b2Vec2(11/PTM_RATIO, -8/PTM_RATIO),b2Vec2(17/PTM_RATIO, -2/PTM_RATIO), b2Vec2(20/PTM_RATIO, 8/PTM_RATIO), b2Vec2(21/PTM_RATIO, 18/PTM_RATIO), b2Vec2(19/PTM_RATIO, 21/PTM_RATIO),
        //        b2Vec2(12/PTM_RATIO, 21/PTM_RATIO),b2Vec2(11/PTM_RATIO, 11/PTM_RATIO), b2Vec2(8/PTM_RATIO, 5/PTM_RATIO), b2Vec2(5/PTM_RATIO, -1/PTM_RATIO), b2Vec2(1/PTM_RATIO, -4/PTM_RATIO),
        //        b2Vec2(-7/PTM_RATIO, -9/PTM_RATIO),b2Vec2(-20/PTM_RATIO, -12/PTM_RATIO),
        
    };
    poly.CreateChain(vertices, 9);
    
    fixtureDef.shape = &poly;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void Thin_circle::removeBody(b2World *_world)
{
    _world->DestroyBody(mainBody);
}

void Thin_circle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

